package com.yanqu.road.server.manager.activity.langyamemorial;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.activity.LangYaMemorialProto;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossLangYaMemorialMgr extends TempCrossMgr {

    private static Map<Integer, LangYaMemorialActivity> activityMap;

    private static Map<Long, Object> userLockerMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {

        if (activityMap == null) {
            activityMap = new ConcurrentHashMap<>();
        }

        // 获取活动
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.LangYaMemorial.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        // 重新加载数据
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (!activityMap.containsKey(activityInfo.getActivityId())) {
                getLangYaMemorialActivity(activityInfo.getActivityId());
            }
        }

        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }


    @Override
    public boolean removeExpireData() throws Exception {
        return true;
    }

    @Override
    public boolean save() {
        if(activityMap != null){
            for (Map.Entry<Integer, LangYaMemorialActivity> entry : activityMap.entrySet()) {
                entry.getValue().save();
            }
        }
        return true;
    }

    public static LangYaMemorialActivity getLangYaMemorialActivity(int activityId) {
        if (activityMap.get(activityId) == null) {
            synchronized (activityMap) {
                if (activityMap.get(activityId) == null) {
                    // 读配置 获取活动信息 保证有活动信息
                    ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
                    if (activityInfo == null) {
                        return null;
                    }
                    LangYaMemorialActivity activity = new LangYaMemorialActivity(activityInfo);
                    activity.init();
                    activityMap.put(activityId, activity);
                }
            }
        }
        return activityMap.get(activityId);
    }

    public static LangYaMemorialProto.LangYaMemorialNoticeRespMsg.Builder getNoticeList(int activityId) {
        LangYaMemorialActivity activity = getLangYaMemorialActivity(activityId);
        if(activity != null){
            return activity.getNoticeList();
        }
        return LangYaMemorialProto.LangYaMemorialNoticeRespMsg.newBuilder().setRet(0);
    }


    public static Object getUserLocker(long userId){
        if(!userLockerMap.containsKey(userId)){
            synchronized (userLockerMap){
                if(!userLockerMap.containsKey(userId)){
                    userLockerMap.put(userId, new Object());
                }
            }
        }
        return userLockerMap.get(userId);
    }

    public static Map<Integer, LangYaMemorialActivity> getActivityMap() {
        return activityMap;
    }
}
